<?php

class ProductsAction extends fvAction {

	function __construct () {
		parent::__construct(fvSite::$Layoult);
	}

	function executeIndex() {
		if (fvRequest::getInstance()->isXmlHttpRequest())
		return self::$FV_NO_LAYOULT;
		else return self::$FV_OK;
	}

	function executeEdit() {
		if (fvRequest::getInstance()->isXmlHttpRequest())
		return self::$FV_AJAX_CALL;
		else return self::$FV_OK;
	}

	function executeSave() {
		$request = fvRequest::getInstance();
		if (!$Products = ProductsManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
			$Products = new Products();
		}
		$n = $request->getRequestParameter('n');
		$Products->updateFromRequest($n);
		if ($Products->save()) {
			if ($n['image'] != '') {
                $Products->saveUploadFile('image', fvSite::$fvConfig->get('path.upload.tmp_dir') . $n['image']);
            }
			fvResponce::getInstance()->setHeader('Id', $Products->getPk());
			$this->setFlash("Данные успешно сохранены", self::$FLASH_SUCCESS);
			if ($request->getRequestParameter('redirect')) {
				fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
			}
		} else {
			fvResponce::getInstance()->setHeader('X-JSON', json_encode($Products->getValidationResult()));
			$this->setFlash("Ошибка при сохранении данных проверьте правильность введенных данных", self::$FLASH_ERROR);
		}

		if (fvRequest::getInstance()->isXmlHttpRequest())
		return self::$FV_AJAX_CALL;
		else return self::$FV_OK;
	}
	function executeDelete() {
		$request = fvRequest::getInstance();
		if (!$Products = ProductsManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
			$this->setFlash("Ошибка при удалении.", self::$FLASH_ERROR);
		} else {
			$Products->deleteImages('image');
			$Products->delete();
			$this->setFlash("Данные успешно удалены", self::$FLASH_SUCCESS);
		}

		fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
		if (fvRequest::getInstance()->isXmlHttpRequest())
		return self::$FV_NO_LAYOULT;
		else return self::$FV_OK;
	}
	
	function executeUploadimage () {
        
        $r = fvRequest::getInstance();
        
        if ($r->isFileUpload('image') && ($r->checkUploadFile('image') == fvRequest::ERROR_SUCCESS)) {
            $tmp_dir = fvSite::$fvConfig->get('path.upload.tmp_dir');
            var_dump($tmp_dir.'1111111');
            $fileName = $r->getUploadFileName('image');
            $r->saveUploadData('image', $tmp_dir . $fileName);

            fvParams::getInstance()->setParameter(fvRoute::getInstance()->getModuleName() . "/filename", $fileName);
        }

        return self::$FV_AJAX_CALL;
    }

    function executeDeleteImage () {
        $Products = ProductsManager::getInstance()->getByPk(fvRequest::getInstance()->getRequestParameter('id'));
        
        if (!is_object($Products) || $Products['image'] != fvRequest::getInstance()->getRequestParameter('image')) {
            if (is_object($Products) && $Products->hasFile('image', fvUploaded::IMAGE_TYPE_SMALL)) {
                fvResponce::getInstance()->setHeader('X-JSON', json_encode(
                    array(
                        'image' => $Products->getImageDir(true) . $Article->getImageName('image', fvUploaded::IMAGE_TYPE_SMALL),
                        'width' => $Products->getImageSize('image', fvUploaded::IMAGE_TYPE_SMALL, fvUploaded::IMAGE_WIDTH),
                        'height' => $Products->getImageSize('image', fvUploaded::IMAGE_TYPE_SMALL, fvUploaded::IMAGE_HEIGHT),
                    )
                ));
            }
            if (is_file(fvSite::$fvConfig->get('path.upload.tmp_name').fvRequest::getInstance()->getRequestParameter('image'))) {
                unlink(fvSite::$fvConfig->get('path.upload.tmp_name').fvRequest::getInstance()->getRequestParameter('image'));
            }
        } else {
            if (is_file(fvSite::$fvConfig->get('path.upload.tmp_name').$Article['image'])) {
                unlink(fvSite::$fvConfig->get('path.upload.tmp_name').$Article['image']);
            }
            $Products->deleteImages('image');
        }
        
        $this->setFlash("Изображение успешно удалено", fvAction::$FLASH_SUCCESS);
        
        return self::$FV_AJAX_CALL;
    }
	

}

?>
